﻿Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Public Class TOURDIADIEM
    Inherits Office2007Form

    Friend WithEvents frmTour As TOUR
    Friend WithEvents frmDiaDiem As DIADIEM
    ' Hidden files are used to store ids 
    Private txtMaTour As DevComponents.DotNetBar.Controls.TextBoxX

    Public Sub InitHiddenFields()
        ' Add a new textbox
        txtMaTour = New DevComponents.DotNetBar.Controls.TextBoxX() With { _
         .Name = "txtMaTour", _
         .Text = "" _
        }
        txtMaTour.Visible = False
    End Sub

    Private Sub btnChonTour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonTour.Click
        frmTour = New TOUR
        frmTour.ShowDialog()
    End Sub

    Private Sub frmTour_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmTour.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        txtMaTour.Text = comp(0) 'id
        txtTour.Text = comp(1) 'name

        LoadData()
    End Sub

    Private Sub TOURDIADIEM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitHiddenFields()
    End Sub

    Private Sub btnRemoveOutOfList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveOutOfList.Click
        If lstvData.SelectedItems.Count > 0 Then
            lstvData.Items.RemoveAt(lstvData.SelectedIndices(0))
        End If
    End Sub

    Private Sub btnAddToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToList.Click
        frmDiaDiem = New DIADIEM
        frmDiaDiem.ShowDialog()
    End Sub

    Private Sub frmDiaDiem_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmDiaDiem.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        Dim objBUS As DIADIEMBUS = New DIADIEMBUS(GlobalVars.g_strConnectionString)
        Dim lstItem As DataTable = objBUS.SelectByID(Integer.Parse(comp(0))).Tables(0)
        If lstItem.Rows.Count > 0 Then
            Dim objListViewItem As ListViewItem = New ListViewItem()
            objListViewItem.Text = comp(0)
            objListViewItem.SubItems.Add(lstItem.Rows(0)("TenDD").ToString())
            objListViewItem.SubItems.Add(lstItem.Rows(0)("DiaChi").ToString())
            objListViewItem.SubItems.Add(lstItem.Rows(0)("QuocGia").ToString())
            lstvData.Items.Add(objListViewItem)
        End If
    End Sub

    Public Function ValidateUserInput(ByRef errorText As String) As Boolean
        If lstvData.Items.Count = 0 Then
            errorText = "Danh sách hiện thời không có gì!"
            Return False
        End If
        Return True
    End Function

    Private Sub btnGhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGhi.Click
        Dim errorText As String = ""
        Dim success As Boolean = False

        If Not ValidateUserInput(errorText) Then
            MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        If ActionUpdate() Then
            success = True
        End If

        If success Then
            MessageBoxEx.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBoxEx.Show("Cập nhật dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Public Sub LoadData()
        Dim objBus As TOUR_DIADIEMBUS = New TOUR_DIADIEMBUS(GlobalVars.g_strConnectionString)
        Dim lstItem As DataTable = objBus.SelectByIDTour(Integer.Parse(txtMaTour.Text)).Tables(0)
        lstvData.Items.Clear()
        If lstItem.Rows.Count > 0 Then
            For i As Integer = 0 To lstItem.Rows.Count - 1
                Dim objListViewItem As ListViewItem = New ListViewItem()
                objListViewItem.Text = lstItem.Rows(i)("IDDiaDiem").ToString()
                objListViewItem.SubItems.Add(lstItem.Rows(i)("TenDD").ToString())
                objListViewItem.SubItems.Add(lstItem.Rows(i)("DiaChi").ToString())
                objListViewItem.SubItems.Add(lstItem.Rows(i)("QuocGia").ToString())
                lstvData.Items.Add(objListViewItem)
            Next
        End If
    End Sub

    Private Function ActionUpdate() As Boolean
        Try
            Dim objBus As TOUR_DIADIEMBUS = New TOUR_DIADIEMBUS(GlobalVars.g_strConnectionString)

            ' Remove all tour dia diem
            If objBus.DeleteByIDTour(Integer.Parse(txtMaTour.Text)) Then
                ' Add new all tour diadiem
                For i As Integer = 0 To lstvData.Items.Count - 1

                    Dim item As TOUR_DIADIEMDTO = New TOUR_DIADIEMDTO()
                    item.IDTour = Integer.Parse(txtMaTour.Text)
                    item.IDDiaDiem = Integer.Parse(lstvData.Items(i).SubItems(0).Text)

                    objBus = New TOUR_DIADIEMBUS(GlobalVars.g_strConnectionString)
                    objBus.Insert(item)
                Next
            End If
            Return True
        Catch
            Return False
        End Try
    End Function

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub
End Class